package kjx_test_security.auth.constans;

/**
 * @ClassName: WhiteApisConfig
 * @Description: api白名单
 * @Author: 郭成兴
 * @Date 2021/9/3 9:06 下午
 * @Copyright 如皋市韶光科技有限公司
 **/
public class WhiteUrlsConstants {

    /**
     * 允许匿名用户访问,不允许已登入用户访问
     */
    public static String[] anonymousUrls = {"/doc.html", "/swagger-ui/index.html", "/index.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**", "/static/**", "/config.js"};

    /**
     * 不管登入不登入,都能访问
     */
    public static String[] permitAllUrls = {
            "/login/doLogin", // 注册
            "/kjx/test"
    };

    public static boolean antMatchersPermit(String url) {
        for (String pattern : permitAllUrls) {
            if (antMatchers(pattern, url)) {
                return true;
            }
        }
        return false;
    }

    private static boolean antMatchers(String pattern, String url) {
        String MATCH_ALL = "/**";
        if (pattern.equals(url)) {
            return true;
        }
        if (pattern.endsWith(MATCH_ALL)) {
            String substring = pattern.substring(0, pattern.length() - 2);
            return url.startsWith(substring);
        }
        return false;
    }
}
